package com.rzj.book.controller;

import com.rzj.book.utils.VCodeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName CheckCodeController
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/16 21:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/checkCode")
public class CheckCodeController extends BaseController{

    @RequestMapping(value = "/getVCode", method = RequestMethod.GET)
    public void getVCode() throws
            Exception {

        HttpSession session = getSession();
        if (session == null) {
            return;
        }
        HttpServletResponse response = getResponse();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String verifyCode = VCodeUtil.generateVerifyCode(4);

        System.out.println("sid"+session.getId());
        session.removeAttribute("checkcode");
        session.setAttribute("checkcode", verifyCode.toLowerCase());
        int w = 100, h = 30;
        VCodeUtil.outputImage(w, h, response.getOutputStream(), verifyCode);
    }
}
